#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import rospy
import json
from flask import Blueprint,request, Response

from parser.account_parser import Account_Parser
from tool.common import *

account_parser = Account_Parser()
account_server = Blueprint("/account",__name__)


#---------------------------------#
#******#   flask-网络端口验证   #******#
#---------------------------------#
@account_server.before_request
def before():
    # print("account_server")
    res_data = before_user(request)
    if res_data:
        return Response(json.dumps(res_data),  mimetype='application/json')



'''
    项目名称：帐号密码相关
    创建时间：2020/12/30
'''
#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 登录
# req: {"name":"dfggb","password":"ffbbh"}
# res: {'code': 0, 'ret': 'ok'}
@account_server.route('/login',methods=['POST'])
def Post_LoginAccount():
    data = request.json
    res_data = account_parser.LoginAccount(data)
    return Response(json.dumps(res_data),mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 帐号查询
# req: None
# res: {'code': 0, 'account_list': [{'password': u'1234', 'name': u'admin', 'perm': u'admin'}, {'password': u'123456', 'name': u'aa', 'perm': u'opera'}], 'ret': 'ok'}
@account_server.route('/account_list',methods=['POST'])
def Post_ListAccount():
    res_data = account_parser.ListAccount()
    return Response(json.dumps(res_data),mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 帐号创建
# req: {"key":"ffbbh","name":"dfggb","new_password":"ffbbh","perm":"admin"}
# res: {'code': 0, 'ret': 'ok'}
@account_server.route('/account_create',methods=['POST'])
def Post_CreateAccount():
    account_data = request.json
    res_data = account_parser.CreateAccount(account_data)
    return Response(json.dumps(res_data),mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 帐号修改
# req: {"key":"ffbbh","name":"dfggb","new_password":"ffbbh","perm":"admin"}
# res: {'code': 0, 'ret': 'ok'}
@account_server.route('/account_modified',methods=['POST'])
def Post_ModifyAccount():
    account_data = request.json
    res_data = account_parser.ModifyAccount(account_data)
    return Response(json.dumps(res_data),mimetype='application/json')

#---------------------------------#
    #******#   POST   #******#
#---------------------------------#
# 帐号删除
# req: {"name": "test"}
# res: {'code': 0, 'ret': 'ok'}
@account_server.route('/account_delete',methods=['POST'])
def Post_DeleteAccount():
    account_data = request.json
    res_data = account_parser.DeleteAccount(account_data)
    return Response(json.dumps(res_data),mimetype='application/json')

